import Vue from 'vue'
import VueRouter from 'vue-router'

// 添加订单 子组件
const OrderList = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/order/List.vue')

// 导入登录子组件
const Login = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Login.vue')
// 导入home子组件
const Home = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Home.vue')
// 导入welcome子组件
const Welcome = () => import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome.vue')

// 导入用户列表组件
const Users = () => import(/* webpackChunkName:"user" */ '../components/users/Users.vue')

// 导入权限管理子组件
const Rights = () => import(/* webpackChunkName:"power" */ '../components/power/Rights.vue')

// 导入权限角色子组件
const Roles = () => import(/* webpackChunkName:"power" */ '../components/power/Roles.vue')

// 导入商品分类子组件
const Cate = () => import(/* webpackChunkName:"goods" */ '../components/goods/Cate.vue')

// 分类参数
const Params = () => import(/* webpackChunkName:"goods" */ '../components/goods/Params.vue')

// 商品列表
const List = () => import(/* webpackChunkName:"goods" */ '../components/goods/List.vue')

// 添加商品
const goodsAdd = () => import(/* webpackChunkName:"goods" */ '../components/goods/Add.vue')
const Report = () => import(/* webpackChunkName:"report" */ '../components/report/Report.vue')

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login }, // 注册 登录子组件
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users }, // 用户列表\
      { path: '/rights', component: Rights },
      { path: '/roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: goodsAdd },
      { path: '/orders', component: OrderList },
      { path: '/reports', component: Report }
    ]
  }

]
// 注册 主页组件
const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // 挂载路由 导航守卫 路由管理器执行之前执行
  if (to.path === '/login') return next()
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) return next('/login')
  next()
})

// 导出路由管理器
export default router
